<template>
  <div v-title :data-title="title">
    <TableContainer
      v-bind="da"
      @selCX="load"
      @reset="reset"
      @getlist="load"
      @simTopRemove="deleteClick"
      @simEdit="editClick"
      @simRemove="deleteOneClick"
    />
  </div>
</template>

<script>
import TableContainer from "@/components/table/TableContainer.vue";
import { selectArticle } from "@/api/article.js";
import { selectComplain, deleteComplain } from "@/api/complain.js";

export default {
  name: "ComplainCon",
  components: {
    TableContainer,
  },
  computed:{
    title(){
      return "举报审核 - news-back";
    }
  },
  data() {
    return {
      da: {
        page: {
          current: 1,
          pageSize: 10,
        },
        TopBtu: [
          {
            type: "danger",
            label: "删除",
            icon: "el-icon-delete-solid",
            incident: "Remove",
          },
        ],
        searchK: [
          { type: "text", label: "举报资讯标题", prop: "title" },
          { type: "text", label: "举报者", prop: "account" },
          { type: "text", label: "举报类型", prop: "complainType" },
        ],
        tableProp: [
          { prop: "id", name: "举报id", width: "200" },
          { prop: "title", name: "举报资讯标题", width: "250" },
          { prop: "account", name: "举报者", width: "150" },
          { prop: "complainType", name: "举报类型", width: "150" },
        ],
        tableData: [],
        tabletotal: 0,
        tableConfig: {
          pagin: true, //是否分页
          selection: true, // 选择框
          index: true, // 序号
          border: true, // 边框
          handle: true, // 是否显示操作列
          handleWidth: "",
          buttonType: "", // buttonType='scope'：使用插槽 slot='table-column-handle' 其他使用buttonAffairs 且如果 button 显示按钮 其他类型显示文字按钮
          buttonAffairs: [
            {
              name: "举报详情",
              icon: "el-icon-plus",
              type: "primary",
              affairs: "Edit",
            },
            {
              name: "删除",
              icon: "el-icon-delete",
              type: "danger",
              affairs: "Remove",
            },
          ],
        },
      },
    };
  },
  mounted() {
    this.load({});
  },
  methods: {
    load(searchform) {
      selectComplain(searchform)
        .then((res) => {
          console.log(res);
          this.da.tableData = res.data.records;
          this.da.tabletotal = res.data.total;
          this.da.page.current = res.data.current;
          this.da.page.pageSize = res.data.size;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    reset() {
      this.load({});
    },
    editClick(row) {
      selectArticle({
          id:row.articleId
          })
        .then((res) => {
          this.$store.commit("SET_MARK", res.data.records[0]);
          this.$store.commit("SET_DEFAULTOPEN","preview")
          this.$router.push({ path: `/publish/${res.data.records[0].id}` });
        })
        .catch((err) => {
          console.log(err);
          this.$message({
              type:"error",
              message:"未找到此篇资讯",
              showClose: true,
          })
        });
      //   this.$store.commit("SET_MARK", row);
      //   this.$router.push({ path: `/publish/${row.id}` });
    },
    deleteClick(data) {
      if (data.length === 0) {
        this.$message({
          showClose: true,
          message: "请选择要删除的举报",
          type: "error",
        });
      } else {
        this.$confirm("此操作将永久删除该举报, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            data.forEach((element) => {
              deleteComplain(element.id)
                .then((res) => {})
                .catch((err) => {
                  console.log(err);
                });
            });
            this.load({});
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    deleteOneClick(data) {
      this.$confirm("此操作将永久删除该举报, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteComplain(data.id)
            .then((res) => {
              if (res.success) {
                this.$message({
                  showClose: true,
                  message: "删除举报成功",
                  type: "success",
                });
              this.load({});
              } else {
                this.$message({
                  showClose: true,
                  message: "删除举报失败",
                  type: "error",
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>